package com.sw.chinesewriteboard.activity;

import android.annotation.SuppressLint;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sw.chinesewriteboard.R;
import com.sw.chinesewriteboard.config.ChineseDataConfig;
import com.sw.chinesewriteboard.config.EnglishDataConfig;
import com.sw.chinesewriteboard.copybook.EnCopyBookActivity;
import com.sw.chinesewriteboard.model.EnglishData;
import com.sw.chinesewriteboard.operation.EnglishDataOperate;

/**
 * @Description:
 * @author: GordenGao
 * @Email: gordengao124@gmail.com
 * @date: 2021年06月23日 16:12
 */
public class EnPractiseCopybookActivity extends EnCopyBookActivity {

    @Override
    protected void initOwnView() {
//        setTitle("英文字母描写帖");
        mEnglishDataOperate.setArrangeType(EnglishDataOperate.ArrangeType.PRACTISE);
        binding.vbnBottomNavigation.initEnvironment(this, false);
        binding.vbnBottomNavigation.setContantHint("请输入生成字帖的字母。例如ABcd");
        binding.vbnBottomNavigation.findViewById(R.id.vbn_ziti_ll_zihao).setVisibility(View.GONE);
        binding.vbnBottomNavigation.findViewById(R.id.vbn_gezi_csl_kuandu).setVisibility(View.GONE);
    }

    protected void refreshCopyBookView() {

        int COLUMN_LENGTH = EnglishDataConfig.TOTAL_LENGTH_COLUMN;
        int ROW_LENGTH = EnglishDataConfig.TOTAL_LENGTH_ROW;

        mWordPaper = new LinearLayout(this);
        mWordPaper.setLayoutParams(new LinearLayout.LayoutParams(ChineseDataConfig.getA4width(this), ChineseDataConfig.getA4height(this)));
        mWordPaper.setGravity(Gravity.CENTER);
        mWordPaper.setAlpha(0f);
        binding.getRoot().addView(mWordPaper);

        mWordList = new LinearLayout(this);
        mWordList.setLayoutParams(new LinearLayout.LayoutParams(WRC, WRC));
        mWordList.setOrientation(LinearLayout.VERTICAL);
        mWordList.removeAllViews();

        mEditTextList.clear();


        mData.setText(mEnglishDataOperate.listToString(mListContent));

        int startIndex = EnglishDataConfig.TOTAL_LENGTH * mPage;    // 字符串的起始坐标
        int k = startIndex;
        System.out.println("日志：p：" + mPage + "    k:" + k);
        for (int i = 0; i < ROW_LENGTH; i++) {
            LinearLayout row = new LinearLayout(this);
            row.setLayoutParams(new LinearLayout.LayoutParams(WRC, WRC));
            row.setOrientation(LinearLayout.HORIZONTAL);
            for (int j = 0; j < COLUMN_LENGTH; j++) {
                View view;
                if (j > 0) {
                    view = createAlphaCharacterLayout(mData.getText().charAt(k), mData);
                } else {
                    view = createCharacterLayout(mData.getText().charAt(k), mData);
                }
                view.setLayoutParams(new LinearLayout.LayoutParams(WRC, WRC));
                row.addView(view);
                k++;
            }
            mWordList.addView(row);
        }
        mWordPaper.addView(mWordList);

        binding.cbIvCopybook.post(new Runnable() {
            @Override
            public void run() {
                binding.cbIvCopybook.setImageBitmap(loadBitmapFromView());
                binding.getRoot().removeView(mWordPaper);
            }
        });
    }

    @SuppressLint("SetTextI18n")
    protected View createAlphaCharacterLayout(char c, EnglishData data) {
        int gridWidth = EnglishDataConfig.getGridWidth(EnPractiseCopybookActivity.this);
        int gridHeight = EnglishDataConfig.getGridHeight(EnPractiseCopybookActivity.this);

        FrameLayout characterLayout = new FrameLayout(this);
        characterLayout.setLayoutParams(new FrameLayout.LayoutParams(gridWidth, gridHeight));

        final TextView pinyin = new TextView(this);
        pinyin.setGravity(Gravity.CENTER);
        pinyin.setEnabled(false);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(gridWidth, gridHeight);
        characterLayout.addView(pinyin, params);

        mCharacterOperation.setCharacterAppearance(pinyin, data);

        if (c != ' ') {
            TextView symbol = new TextView(this);
            symbol.setText(c + "");
            symbol.setTextSize(data.getTextSize());
            symbol.setTextColor(data.getFontColor().getColor());
            symbol.setTypeface(data.getMyTypeface().getTypeface());
            symbol.setAlpha(data.getAlpha());
            symbol.setGravity(Gravity.CENTER);
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(gridWidth, gridHeight);
            characterLayout.addView(symbol, layoutParams);
        }

        return characterLayout;
    }
}
